package com.shuorikeji.huohuo.ui.sj;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.MhHotTagAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.MhHotTagInfoBean;
import com.shuorikeji.huohuo.bean.MhHotTagMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.JsonUtil;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.shuorikeji.huohuo.view.GridDividerItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MhHotTagActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_tag_count)
    TextView tvTagCount;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlView;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.tv_sec_tag)
    TextView tvSecTag;

    private int curPage;
    private List<MhHotTagInfoBean> tagList = new ArrayList<>();
    private List<MhHotTagInfoBean> secTagList = new ArrayList<>();
    private MhHotTagAdapter mhHotTagAdapter;
    private int secTagCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_mh_hot_tag);
    }

    @Override
    protected void initData() {
        secTagList.clear();
        secTagCount = 0;
        curPage = 1;
        initSmartRefreshLayout();
        initAdapter();
        getMhHotTag();
        initClick();
    }

    private void initSmartRefreshLayout() {
        srlView.setRefreshHeader(new ClassicsHeader(MhHotTagActivity.this));
        srlView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPage = 1;
                getMhHotTag();
            }
        });
        srlView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPage = curPage + 1;
                getMhHotTag();
            }
        });
    }

    private void initAdapter() {
        FlexboxLayoutManager manager = new FlexboxLayoutManager(this);
        manager.setFlexDirection(FlexDirection.ROW);
        manager.setFlexWrap(FlexWrap.WRAP);
        rvData.addItemDecoration(new GridDividerItemDecoration(20));
        rvData.setLayoutManager(manager);
        mhHotTagAdapter = new MhHotTagAdapter(MhHotTagActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        secTagCount = 0;
                        secTagList.clear();
                        MhHotTagInfoBean data = (MhHotTagInfoBean) object;
                        for (int i = 0; i < tagList.size(); i++) {
                            if (data.getId() == tagList.get(i).getId()) {
                                if (tagList.get(i).isSec()) {
                                    tagList.get(i).setSec(false);
                                } else {
                                    tagList.get(i).setSec(true);
                                }
                                break;
                            }
                        }
                        for (int i = 0; i < tagList.size(); i++) {
                            if (tagList.get(i).isSec()) {
                                secTagCount = secTagCount + 1;
                                secTagList.add(tagList.get(i));
                            }
                        }
                        tvTagCount.setText("点击选择标签，当前已选择" + secTagCount + "个");
                        mhHotTagAdapter.setData(tagList);
                        break;
                }
            }
        });
        rvData.setAdapter(mhHotTagAdapter);
    }

    private void getMhHotTag() {
        mPresenter.mhHotTag(true, 20, curPage, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                srlView.finishRefresh();
                srlView.finishLoadMore();
                switch (flag) {
                    case 1:
                        MhHotTagMainBean data = (MhHotTagMainBean) object;
                        if (curPage == 1) {
                            tagList.clear();
                        }
                        tagList.addAll(data.getData().getData());
                        mhHotTagAdapter.setData(tagList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhHotTagActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        tvSecTag.setOnClickListener(view -> {
            if (secTagCount == 0) {
                ToastShowUtil.showToastCenter(MhHotTagActivity.this, "请选择标签");
            } else {
                Intent intent = new Intent();
                intent.putExtra("SEC_TAG", JsonUtil.toJson(secTagList));
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}